/*
 * Created on Feb 17, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.model;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.util.*;
import java.io.File;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import ModuleBuilder.UI.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ImageCacheDelegate {
	static HashMap allImages = new HashMap();
	
//	public static ImageElement loadImage( String fn ) {
//		ImageElement el=null;
//		
//		// first check the cache to see if this has been loaded already.
//		Object val = allImages.get(fn);
//		if ( val != null ) {
//			// this is already cached, return it
//			el = (ImageElement)val;
//		} else {
//			// this is a new file, load it from the file system	
//			File file = new File( fn );
//			el = loadImage(file);
//			allImages.put(fn, el);
//		}
//		return( el );
//	}
	
	// file is garanteed to be NOT null
	//
	public static ImageElement loadImage(File file) {
		String fn = file.getPath();
		// first check the cache to see if this has been loaded already.
		Object val = allImages.get(fn);
		ImageElement el=null;
		
		if ( val != null ) {
			// this is already cached, return it
			el = (ImageElement)val;
		} else {
			// this is a new file, load it from the file system	
		
			ImageIcon tmpIcon=null, thumbnail=null;
			
			tmpIcon = new ImageIcon(file.getPath());
			if (tmpIcon != null) {
				thumbnail = makeSmallIconFromLarge(tmpIcon);
			}
			el = new ImageElement(thumbnail, tmpIcon);
			allImages.put(fn, el);
		}
		return ( el );
	}

	public static ImageIcon makeSmallIconFromLarge( ImageIcon tmpIcon ) {
		// depends on if height or width is larger, determine final size acordingly.
		ImageIcon smallIcon=null;
		
		int h = tmpIcon.getIconHeight();
		int w = tmpIcon.getIconWidth();
							
		if ((w > ImagePreview._previewIconSize) || (h > ImagePreview._previewIconSize )){
			// either the hight is larger or the width
			// then we need to pick out the larger one as the constraint
			//
			if ( w > h ) {
				smallIcon = new ImageIcon(	tmpIcon.getImage().
										getScaledInstance(
											ImagePreview._previewIconSize, 
											-1,
											Image.SCALE_DEFAULT  ));
			} else {
				// the height is greater				
				smallIcon = new ImageIcon(	tmpIcon.getImage().
										getScaledInstance(												 
											-1,
											ImagePreview._previewIconSize,
											Image.SCALE_DEFAULT  ));
			}
		} else { //no need to miniaturize
			smallIcon = tmpIcon;
		}
		return ( smallIcon );			
	}
	
	public static void removeImageElement( String fn ) {
		allImages.remove(fn);
	}
	
	public static ImageElement getImageElement( String fn ) {
		return ( (ImageElement)allImages.get(fn));
	}
	
	public static void addImageElement(String fn, ImageElement img) {
		allImages.put(fn, img);
		return;
	}

}
